<?php
namespace App\Services\WxCardMemberLevel\Factory;

use App\Models\WxCardMemberList;
use App\Services\WxCard\Factory\WxCardMemberCard;
use App\Services\WxMaterial\Factory\WxMaterialNews;

/**
 * 通用类会员卡-VIP会员业务类
 * @package App\Services\WxCardMemberLevel
 */
class CommonCardVip extends MemberLevelAbstract
{
    /**
     * 检查判断会员等级是否有效，并返回该等级对应的数 据
     * @param $userOpenId
     * @return bool
     * @throws \Exception
     */
    public function checkValid($userOpenId)
    {
        $isValid = false;
        $memberListModel = WxCardMemberList::where('appid', $this->appid)->where('openid', $userOpenId)->where('card_base_id', $this->levelData['card_base_id'])->first();
        $memberListData = $memberListModel ? $memberListModel->toArray() : [];
        if (!empty($memberListData)) {
            $memberListData['user_info'] = (new WxCardMemberCard($this->appid))->fomatUserInfo($memberListData['user_info']);
            //查询用户手机号
            $mobile = !empty($memberListData['user_info']['mobile']) ? $memberListData['user_info']['mobile'] : '';
            if ($mobile){
                //TODO::判断是否符合该会员等级条件
                $isValid = true;
            }
            if ($isValid){
                //将该等级会员卡背景转换为微信地址
                if (!empty($this->levelData['background_pic_url'])){
                    $this->levelData['background_pic_url'] = (new WxMaterialNews($this->appid))->getWxViewUrl($this->levelData['background_pic_url']);
                }

                return $this->levelData;
            }
        }
        return false;
    }
}